package com.zqxq.lottery.service.order.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.constant.Result;
import com.zqxq.common.utils.DateUtils;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.controller.user.UserFundsController;
import com.zqxq.lottery.entity.o2o.order.UserOrderO2O;
import com.zqxq.lottery.entity.po.export.ExpertInfoPO;
import com.zqxq.lottery.entity.po.jczq.JczqMatchPO;
import com.zqxq.lottery.entity.po.order.FollowOrderPO;
import com.zqxq.lottery.entity.po.order.RobotOrderDataPO;
import com.zqxq.lottery.entity.po.order.UserOrderBetPO;
import com.zqxq.lottery.entity.po.order.UserOrderPO;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.entity.po.user.UserInfoPO;
import com.zqxq.lottery.entity.vo.expert.ExpertDetailVO;
import com.zqxq.lottery.entity.vo.order.*;
import com.zqxq.lottery.mapper.expert.ExpertFollowMapper;
import com.zqxq.lottery.mapper.expert.ExpertInfoMapper;
import com.zqxq.lottery.mapper.jczq.JczqMatchMapper;
import com.zqxq.lottery.mapper.order.FollowOrderMapper;
import com.zqxq.lottery.mapper.order.RobotOrderDataMapper;
import com.zqxq.lottery.mapper.order.UserOrderBetMapper;
import com.zqxq.lottery.mapper.order.UserOrderMapper;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.service.order.IFollowOrderService;
import com.zqxq.lottery.service.order.IUserOrderService;
import com.zqxq.lottery.service.user.IUserFundsService;
import com.zqxq.lottery.util.CalculatorPL;
import com.zqxq.lottery.util.SelfStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author 苦智
 * @date 2018/8/3 17:51
 * @description
 * @ClassName FollowOrderServiceImpl
 */
@Service
@Slf4j
public class FollowOrderServiceImpl extends ServiceImpl<FollowOrderMapper,FollowOrderPO> implements IFollowOrderService{
    @Autowired
    private ExpertFollowMapper expertFollowMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private IUserOrderService userOrderService;
    @Autowired
    private ExpertInfoMapper expertInfoMapper;
    @Autowired
    private JczqMatchMapper jczqMatchMapper;
    @Autowired
    private IUserFundsService userFundsService;
    @Autowired
    private UserOrderBetMapper userOrderBetMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<FollowOrderVO> getFollowOrderList(String userId, String orderId, Integer type,String followUserId, Page<FollowOrderVO> page) {
        List<FollowOrderVO> list = new ArrayList();
        switch (type){
            case 4:
                list = baseMapper.queryFunsCount(followUserId,page);
                for (FollowOrderVO followOrderVO : list) {
                    String last30DayStatus = baseMapper.getLast30DayStatusByUserId(followOrderVO.getUserId());
                    followOrderVO.setReds(SelfStringUtils.getMaxContinueNum(last30DayStatus,"1"));
                }
                break;
            default:
                list = baseMapper.selectFollowOrderList(userId,orderId,type,page);
                for (FollowOrderVO followOrderVO : list) {
                    followOrderVO.setSP(userOrderService.getSP(Long.valueOf(followOrderVO.getOrderId())));
                    packagePushOrderData(followOrderVO);
                }
                if (type == 1)
                    SelfStringUtils.sortByAttribute(list,type);
        }
        return list;
    }

    private void packagePushOrderData(FollowOrderVO followOrderVO) {
        //包装跟单数据
        ExpertInfoPO expertInfoPO = new ExpertInfoPO();
        expertInfoPO.setUser_id(Long.valueOf(followOrderVO.getUserId()));
        expertInfoPO.setType(1);
        expertInfoPO = expertInfoMapper.selectOne(expertInfoPO);
        if (expertInfoPO != null){
            Integer follows = followOrderVO.getFollows();
            String f = stringRedisTemplate.opsForValue().get("ROBOT" + followOrderVO.getOrderId());
            if (StrUtils.isNotEmpty(f)){
                Integer intF = Integer.valueOf(f);
                follows+= intF;
                BigDecimal minFollowAmt = followOrderVO.getMinFollowAmt();
                BigDecimal followAmt = followOrderVO.getFollowAmt().add(minFollowAmt.multiply(new BigDecimal(intF)));
                followOrderVO.setFollows(follows);
                followOrderVO.setFollowAmt(followAmt);
            }
        }
    }

    /**
    * @Author 苦智
    * @Description 推单列表
    * @Date 16:31 2018/8/8
    * @Param [userId, type, status] 用户id、type：1：近7日2：近30日、status：1：中奖推单2：待开推单3：未中推单
    * @return com.zqxq.lottery.entity.vo.order.PushOrderVO
    **/
    @Override
    public PushOrderVO getPushOrder(String userId, Integer type, Integer status,Integer pageNo,Integer pageSize) {
        PushOrderVO pushOrderVO = baseMapper.getPushOrderCount(userId,type);
        if (pushOrderVO != null){
            //连红数
            Integer num = SelfStringUtils.getMaxContinueNum(pushOrderVO.getPrizeStatusStr(), "1");
            pushOrderVO.setReds(num);
            Page<PushOrderDetailVO> page = new Page(pageNo,pageSize);
            List<PushOrderDetailVO> l = baseMapper.getPushOrderList(userId,type,status,page);
            for (PushOrderDetailVO pushOrderDetailVO : l) {
                packagePushOrderByUserId(userId, pushOrderDetailVO);

            }
            page.setRecords(l);
            pushOrderVO.setOrderPage(page);
        }
        return pushOrderVO;
    }

    private void packagePushOrderByUserId(String userId, PushOrderDetailVO pushOrderDetailVO) {
        //包装跟单数据
        if (userId.split(",").length>1){
            userId = userId.split(",")[0];
        }
        ExpertInfoPO expertInfoPO = new ExpertInfoPO();
        expertInfoPO.setUser_id(Long.valueOf(userId));
        expertInfoPO.setType(1);
        expertInfoPO = expertInfoMapper.selectOne(expertInfoPO);
        if (expertInfoPO != null){
            Integer follows = pushOrderDetailVO.getFollows();
            String f = stringRedisTemplate.opsForValue().get("ROBOT" + pushOrderDetailVO.getOrderId());
            if (StrUtils.isNotEmpty(f)){
                Integer intF = Integer.valueOf(f);
                follows+= intF;
                BigDecimal minFollowAmt = pushOrderDetailVO.getMinFollowAmt();
                BigDecimal followAmt = pushOrderDetailVO.getFollowAmt().add(minFollowAmt.multiply(new BigDecimal(intF)));
                pushOrderDetailVO.setFollows(follows);
                pushOrderDetailVO.setFollowAmt(followAmt);
            }
        }
    }

    @Override
    public ExpertDetailVO getExpertDetail(String userId, String followUserId) {
        ExpertDetailVO expertDetailVO = baseMapper.getExpertInfo(userId,followUserId);
        ExpertDetailVO detailVO = baseMapper.getProfitOddsByUserId(2,userId);
        String lastStatus = getLastStatus(userId);
        expertDetailVO.setProfitOdds(detailVO.getProfitOdds());
        expertDetailVO.setHitOdds(detailVO.getHitOdds());
        expertDetailVO.setHitNums(detailVO.getHitNums());
        expertDetailVO.setCreateOrders(detailVO.getCreateOrders());
        expertDetailVO.setLastStatus(lastStatus);
        List<PushOrderDetailVO> list = baseMapper.getfollowOrderListByExpertId(userId);
        if (expertDetailVO != null){
            for (PushOrderDetailVO pushOrderDetailVO : list) {
                //包装跟单数据
                packagePushOrderByUserId(userId, pushOrderDetailVO);
            }
            expertDetailVO.setOrderList(list);
        }
        return expertDetailVO;
    }

    @Override
    public ExpertDetailVO getPushOrderDetailByPid(String orderId, String expertId,String followUserId) {
        ExpertDetailVO expertDetailVO = baseMapper.getExpertInfo(expertId,followUserId);
        ExpertDetailVO detailVO = baseMapper.getProfitOddsByUserId(2,expertId);
        String lastStatus = getLastStatus(expertId);
        expertDetailVO.setProfitOdds(detailVO.getProfitOdds());
        expertDetailVO.setHitOdds(detailVO.getHitOdds());
        expertDetailVO.setHitNums(detailVO.getHitNums());
        expertDetailVO.setCreateOrders(detailVO.getCreateOrders());
        expertDetailVO.setLastStatus(lastStatus);
        PushOrderDetailVO pushOrderDetailVO = baseMapper.getPushOrderDetailByPid(orderId);
        if (expertDetailVO != null){
            if(StringUtils.isEmpty(expertDetailVO.getIntroduce())){
                expertDetailVO.setIntroduce("稳如哈马比，一起红！");
            }
            pushOrderDetailVO.setSp(userOrderService.getSP(Long.valueOf(orderId)));
            //包装跟单数据
            packagePushOrderByUserId(expertId, pushOrderDetailVO);
            expertDetailVO.setPushOrderDetailVO(pushOrderDetailVO);
        }
        return expertDetailVO;
    }

    /**
    * @Author 苦智
    * @Description 推单机器人人气塑造
    * @Date 10:55 2018/11/30
    * @Param []
    * @return void
    **/
    @Override
    public void createRobotPropularity(){
        try {
            synchronized (this){
                //查询推单机器人订单
                List<UserOrderPO> orderList = baseMapper.getRobotPushOrder();
                if (orderList != null && orderList.size()>0) {
                    for (UserOrderPO userOrderPO : orderList) {
                        //推单成功塑造人气
                        long l = userOrderPO.getLottery_time().getTime() - new Date().getTime();
                        if (l > 0) {
                            String follows = stringRedisTemplate.opsForValue().get("ROBOT" + userOrderPO.getId());
                            if (!StrUtils.isEmpty(follows)) {
                                int f = Integer.valueOf(follows);
                                SysConfigPO val = sysConfigMapper.getByKey("FOLLOW_ORDER_ROBOT");
                                //每推单时间推单数
                                int sysFollows = Integer.parseInt(JSONObject.parseObject(val.getCval(), Map.class).get("FOLLOWS").toString());
                                //推单限制
                                String userFollows = stringRedisTemplate.opsForValue().get("FOLLOWS" + userOrderPO.getUser_id());
                                if (!StrUtils.isEmpty(userFollows)){
                                    sysFollows = Integer.valueOf(userFollows);
                                }
                                // 假数据最多50条
                                if (f < sysFollows) {
                                    Random r = new Random();
                                    f += r.nextInt(4) + 1;
                                    stringRedisTemplate.opsForValue().set("ROBOT" + userOrderPO.getId(), f + "");
                                }
                            } else {
                                stringRedisTemplate.opsForValue().set("ROBOT" + userOrderPO.getId(), "1");
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("推单塑造人气异常",e);
        }


    }


    @Override
    public void followOrderRobot() {
        try {
            synchronized (this) {
                Random random = new Random();
                int pushOrders = baseMapper.getPushOrdersToday();
                SysConfigPO val = sysConfigMapper.getByKey("FOLLOW_ORDER_ROBOT");
                //每推单时间推单数
                int per_orders = Integer.parseInt(JSONObject.parseObject(val.getCval(), Map.class).get("PER_ORDERS").toString());
                int orders = Integer.parseInt(JSONObject.parseObject(val.getCval(), Map.class).get("ORDERS").toString());
                int ordersPerPeople = Integer.parseInt(JSONObject.parseObject(val.getCval(), Map.class).get("PER_PERSON_ORDERS").toString());
                //获取机器人账号
                List<String> expertList = expertInfoMapper.selectExpertIdListByType(ordersPerPeople);
                if (expertList != null && expertList.size() > 0 && pushOrders < orders) {
                    for (int i = 0; i < per_orders; i++) {
                        String user_id = getRandomUser(random, expertList);
                        if (StrUtils.isNotEmpty(user_id)) {
                            UserOrderVO userOrderVO = robotCreateOrder(random, user_id);
                            if (userOrderVO != null) {
                                //设置每个专家人气最高值
                                int f = random.nextInt(40) + 3;
                                long l = DateUtils.strToDate(userOrderVO.getLotteryTime()).getTime() - new Date().getTime();
                                String s = stringRedisTemplate.opsForValue().get("FOLLOWS" + user_id);
                                if (StrUtils.isEmpty(s))
                                    stringRedisTemplate.opsForValue().set("FOLLOWS" + user_id, f + "", l, TimeUnit.MILLISECONDS);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("机器人推单异常",e);
        }
    }

    private String getRandomUser(Random random,List<String> expertList) {
        String user_id = expertList.get(random.nextInt(expertList.size()));
        int count = baseMapper.selectFollowCount(user_id);
        if (count > 2) {
            if (expertList.size() == 1)
                user_id = "";
            else {
                expertList.remove(user_id);
                getRandomUser(random, expertList);
            }
        }
        return user_id;
    }

    private UserOrderVO robotCreateOrder(Random random, String user_id) throws Exception {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("user_id",user_id);
        wrapper.eq("ticket_status",1);
        wrapper.eq("follow_status",2);
        int count = userOrderService.selectCount(wrapper);
        String commission = "0";
        if (count>2)
            commission = "10";
        //选择一场最近的比赛
        List<JczqMatchPO> jczqMatchPOList= jczqMatchMapper.getLastMatch(1);
        if (jczqMatchPOList != null && jczqMatchPOList.size() < 7){
            jczqMatchPOList= jczqMatchMapper.getLastMatch(2);
        }
        //选取包含单关的场次
        List<JczqMatchPO> jczqPassMatchPOList = jczqMatchMapper.getPassMatch();
        if ((jczqMatchPOList == null || jczqMatchPOList.size() < 3) && (jczqPassMatchPOList == null && jczqPassMatchPOList.size() < 1))
            return null;
        int choose = 0;
        if (jczqPassMatchPOList != null && jczqPassMatchPOList.size() > 0){
            choose = random.nextInt(2);
        }
        List<UserOrderBetVO> l = new ArrayList();
        UserOrderVO userOrderVO = new UserOrderVO();
        int betTimes = 0;
        BigDecimal payAmt = BigDecimal.ZERO;
        String bet_bunch = "";
        String bet_num = "1";
        if (choose == 1){
            //发布单关
            l = getUserOrderBetListDg(user_id, jczqPassMatchPOList);
            if (l.size() == 1){
                betTimes = random.nextInt(26) + 25;
                payAmt = new BigDecimal(200 * betTimes);
            }else if (l.size() == 2){
                betTimes = random.nextInt(13) + 13;
                payAmt = new BigDecimal(400 * betTimes);
                bet_num = "2";
            }
            bet_bunch = "1-1";
        }else {
            //发布二串一
            CalculatorPL calculatorPL = new CalculatorPL();
            long recursionNum = calculatorPL.combine(jczqMatchPOList.size(), 2);//方案个数
            l = getUserOrderBetListGg(user_id, jczqMatchPOList, recursionNum);
            betTimes = random.nextInt(26) + 25;
            payAmt = new BigDecimal(200 * betTimes);
            bet_bunch = "2-1";
        }
        if (l.size() < 1 || betTimes == 0)
            return null;
        userOrderVO.setOrderBetList(l);
        userOrderVO.setFollowStatus("2");
        userOrderVO.setCommissionRate(commission);
        userOrderVO.setBetTimes(betTimes + "");//投注倍数 25~50 随机数
        userOrderVO.setPayAmount(payAmt.toString());//投注金额
        userOrderVO.setUserId(user_id);//随机获取机器人账号
        userOrderVO.setLotteryId("21");
        userOrderVO.setPeriod("21");
        userOrderVO.setPayWay("4");
        userOrderVO.setBetNum(bet_num);//注数
        userOrderVO.setBetBunch(bet_bunch);
        userOrderVO.setHeelMultiple("10");
        userOrderVO.setSecrecySet("1");
        userOrderVO.setBuyWay("0");
        String lotteryTime  = l.get(0).getLotteryTime();
        if (l.size()>1 && getDateDiff(l.get(0).getLotteryTime(), l.get(1).getLotteryTime()) > 0)
            lotteryTime = l.get(1).getLotteryTime();
        userOrderVO.setLotteryTime(lotteryTime);//预计开奖时间
        userOrderVO.setIsPublishFollow("0");
        UserOrderVO orderVO = userOrderService.insertOrder(userOrderVO);
        //支付订单
        UserOrderPO orderPO = userOrderService.selectById(orderVO.getId());
        Result result = userFundsService.payOrder(UserOrderO2O.toVO(orderPO),null);
        if (result.success())
            return orderVO;
        else
            return null;
    }

    private List<UserOrderBetVO> getUserOrderBetListDg(String user_id, List<JczqMatchPO> jczqPassMatchPOList) {
        List<UserOrderBetVO> userOrderBetVOList = new ArrayList();
        if (jczqPassMatchPOList.size() == 1){
            JczqMatchPO jczqMatchPO = jczqPassMatchPOList.get(0);
            UserOrderBetVO userOrderBetVO = getUserOrderBetVOPass(jczqMatchPO);
            userOrderBetVOList.add(userOrderBetVO);
        }else if (jczqPassMatchPOList.size() >=2){
            List<Integer> randomNum = getRandomNum(0, 2, jczqPassMatchPOList.size());
            JczqMatchPO jczqMatchPO1 = jczqPassMatchPOList.get(randomNum.get(0));
            UserOrderBetVO userOrderBetVO1 = getUserOrderBetVOPass(jczqMatchPO1);
            userOrderBetVOList.add(userOrderBetVO1);
            JczqMatchPO jczqMatchPO2 = jczqPassMatchPOList.get(randomNum.get(1));
            UserOrderBetVO userOrderBetVO2 = getUserOrderBetVOPass(jczqMatchPO2);
            if(!userOrderBetVO2.getPlayType().equals(userOrderBetVO1.getPlayType())){
                userOrderBetVO1.setPlayType("6");
                userOrderBetVO2.setPlayType("6");
            }
            userOrderBetVOList.add(userOrderBetVO2);
        }
        return userOrderBetVOList;
    }

    private static UserOrderBetVO getUserOrderBetVOPass(JczqMatchPO jczqMatchPO) {
        Map oddsMap = JSONObject.parseObject(jczqMatchPO.getOdds(), Map.class);
        String[] passArr = jczqMatchPO.getPass_status().split(",");
        String odds = "";
        List<String> l = new ArrayList();
        boolean flag = true;
        if ("1".equals(passArr[0])){
            l.add(oddsMap.get("0").toString());
            l.add(oddsMap.get("1").toString());
            l.add(oddsMap.get("2").toString());
            flag = false;
        }else{
            l.add(oddsMap.get("3").toString());
            l.add(oddsMap.get("4").toString());
            l.add(oddsMap.get("5").toString());
        }
        String minOdds = Collections.min(l);
        int redNum = l.indexOf(minOdds);
        if (flag)
            redNum = l.indexOf(minOdds)+3;
        UserOrderBetVO userOrderBetVO = new UserOrderBetVO();
        userOrderBetVO.setPeriod(jczqMatchPO.getCode());//竞彩的match_field
        userOrderBetVO.setRedNum(redNum+"");//投注号
        userOrderBetVO.setBlueNum(minOdds);//投注赔率
        userOrderBetVO.setLotteryTime(DateUtils.dateToStr(jczqMatchPO.getClose_time(),"yyyy-MM-dd HH:mm:ss"));
        String playType = "1";
        if (flag){
            playType = "2";
        }
        userOrderBetVO.setPlayType(playType);//玩法 1：胜平负；2：让球胜平负；3：比分；4：总进球；5：半全场；6：混合投注；7：单关固定
        return userOrderBetVO;
    }

    private List<Integer> getRandomNum(int begin,int size,int maxNum){
        Random r=new Random();
        Set<Integer> ranNum=new HashSet<Integer>();
        while(ranNum.size()<size){
            ranNum.add(r.nextInt(maxNum)+begin);
        }
        return new ArrayList(ranNum);
    }

    //机器人推单bet列表 (方案去重)
    private List<UserOrderBetVO> getUserOrderBetListGg(String user_id,List<JczqMatchPO> jczqMatchPOList,Long recursionNum) {
        //获取推单人历史推单方案
        Random random = new Random();
        List<UserOrderBetVO> l = new ArrayList();
        List<Integer> randomNum = getRandomNum(0, 2, jczqMatchPOList.size());
        int i1 = randomNum.get(0);
        int i2 = randomNum.get(1);
        JczqMatchPO jczqMatchPO1= jczqMatchPOList.get(i1);
        JczqMatchPO jczqMatchPO2= jczqMatchPOList.get(i2);
        UserOrderBetVO userOrderBetVO1 = getUserOrderBetVO(jczqMatchPO1);
        UserOrderBetVO userOrderBetVO2 = getUserOrderBetVO(jczqMatchPO2);
//        if (userOrderBetVO1.getPeriod().equals(userOrderBetVO2.getPeriod())){
//            getUserOrderBetList(user_id,jczqMatchPOList,recursionNum);
//        }
        recursionNum--;
        boolean f = true;
        //同一用户不能有相同的推单方案
        List<Map<String,String>> programmeList = userOrderBetMapper.getPushOrderProgramme(user_id);
        for (Map<String, String> programme : programmeList) {
            String period = programme.get("period");
            String redNum = programme.get("redNum");
            int flag = compareStr(period, userOrderBetVO1.getPeriod() + "," + userOrderBetVO2.getPeriod());
            int flag1 = compareStr(redNum, userOrderBetVO1.getRedNum() + "," + userOrderBetVO2.getRedNum());
            if (flag == 1 && flag1 == 1) {
                f = false;
                break;
            }
        }
        if (f){
            l.add(userOrderBetVO1);
            l.add(userOrderBetVO2);
        }else {
            if (recursionNum > 0)
                getUserOrderBetListGg(user_id,jczqMatchPOList,recursionNum);
        }
        return l;
    }

    //比较字符串 1 相同 0：不同
    private static Integer compareStr(String str1, String str2) {
        String[] al = str1.split(",");
        int flag = 1;
        for (String s : al) {
            if (!str2.contains(s)){
                flag = 0;
                break;
            }
        }
        return flag;
    }

    //封装orderbet
    private UserOrderBetVO getUserOrderBetVO(JczqMatchPO jczqMatchPO) {
        UserOrderBetVO userOrderBetVO = new UserOrderBetVO();
        String aOdds = jczqMatchPO.getOdds();
        String odd = getMinOdd(aOdds);
        String redNum = getRedNum(odd,aOdds);
        userOrderBetVO.setPeriod(jczqMatchPO.getCode());//竞彩的match_field
        userOrderBetVO.setRedNum(redNum);//投注号
        userOrderBetVO.setBlueNum(odd);//投注赔率
        userOrderBetVO.setLotteryTime(DateUtils.dateToStr(jczqMatchPO.getClose_time(),"yyyy-MM-dd HH:mm:ss"));
        String playType = "2";
        if (Integer.parseInt(redNum) >= 0 && Integer.parseInt(redNum) <3){
            playType = "1";
        }
        userOrderBetVO.setPlayType(playType);//玩法 1：胜平负；2：让球胜平负；3：比分；4：总进球；5：半全场；6：混合投注；7：单关固定
        return userOrderBetVO;
    }

    //获取最小赔率
    public static String getMinOdd(String odds) {
        Map map = JSONObject.parseObject(odds, Map.class);
        Collection values = map.values();
        Comparable min = (Comparable) Collections.min(values);
        return min.toString();
    }

    //获取投注号码
    public static String getRedNum(String odd,String odds) {
        String o = odds.replace("{", "").replace("}", "");
        String[] oArr = o.split(",");
        int i = 0;
        for (;i<oArr.length;i++) {
            Map map = JSONObject.parseObject(odds, Map.class);
            if (odd.equals(map.get(i+""))){
                break;
            }
        }
        return i+"";
    }

    public String getLastStatus(String expertId){
        String str = baseMapper.getLastStatusByUserId(expertId);
        if (StrUtils.isEmpty(str))
            return "";
        StringBuilder builder = new StringBuilder(str);
        return builder.reverse().toString();
    }

    public static long getDateDiff(String startDate, String endDate) {
        long diff = 0L;

        try {
            Date date1 = (new SimpleDateFormat("yyyy-MM-dd")).parse(startDate);
            Date date2 = (new SimpleDateFormat("yyyy-MM-dd")).parse(endDate);
            diff = (date1.getTime() - date2.getTime()) / 86400000L;
        } catch (ParseException var6) {
            ;
        }

        return diff;
    }


}
